package com.controller.order;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.order.GetOrderTaskInfoDO;
import com.model.dto.order.task.GetOrderTaskInfoAllDTO;
import com.model.dto.order.task.GetOrderTaskInfoListDTO;
import com.model.pojo.base.BaseCarInfo;
import com.model.pojo.order.OrderTaskInfo;
import com.model.vo.order.task.info.GetOrderTaskInfoVO;
import com.service.base.BaseCarInfoService;
import com.service.order.OrderTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zql
 * @Description: 任务订单记录模块
 * @Data: 2023-10-22
 */
@Api(tags = {"任务订单记录"})
@Slf4j
@RestController
@RequestMapping("/v1/order/taskInfo")
@RequiredArgsConstructor
public class OrderTaskInfoController {
    private final   OrderTaskInfoService orderTaskInfoService;
    private final BaseCarInfoService baseCarInfoService;
    /**
    * @Author: zql
    * @Description: 任务订单记录列表
     * @param getOrderTaskInfoListDTO 请求数据
     * @Data: 2023-10-22
     */
    @Log(title = "任务订单记录-任务订单记录列表", businessType = BusinessType.SELECT,  isSaveResponseData = false)
    @ApiOperation(value = "任务订单记录列表")
    @PostMapping("/getOrderTaskInfoList")
    public Result<PageResult<List<GetOrderTaskInfoVO>>> getOrderTaskInfoList(@RequestBody @Valid GetOrderTaskInfoListDTO getOrderTaskInfoListDTO) {
        GetOrderTaskInfoDO getOrderTaskInfoDO = EntityConversionUtil.copy(getOrderTaskInfoListDTO, GetOrderTaskInfoDO.class);
        List<OrderTaskInfo> orderTaskInfoList = orderTaskInfoService.getList(getOrderTaskInfoDO);
        PageInfo<OrderTaskInfo> pageInfo = new PageInfo<>(orderTaskInfoList);
        List<GetOrderTaskInfoVO> getOrderTaskInfoVOList = EntityConversionUtil.copyList(orderTaskInfoList, GetOrderTaskInfoVO.class);
        List<Long> carIdList = getOrderTaskInfoVOList.stream().map(GetOrderTaskInfoVO::getCarId).collect(Collectors.toList());
        //车辆信息
        List<BaseCarInfo> baseCarInfoList = baseCarInfoService.getCarIdList(carIdList);
        getOrderTaskInfoVOList.forEach(item -> {
            BaseCarInfo baseCarInfo = baseCarInfoList.stream().filter(s -> s.getCarId().equals(item.getCarId())).collect(Collectors.toList()).get(0);
            item.setCarNo(baseCarInfo.getCarNo());
            item.setPlateNumber(baseCarInfo.getPlateNumber());
            item.setLat(baseCarInfo.getLat());
            item.setLon(baseCarInfo.getLon());
            item.setAddress(baseCarInfo.getAddress());
        });
        PageResult<List<GetOrderTaskInfoVO>> build = PageResult.<List<GetOrderTaskInfoVO>>builder().result(getOrderTaskInfoVOList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }


    /**
     * @Author: zql
     * @Description: 任务订单记录所有列表
     * @param dto 请求数据
     * @Data: 2023-10-22
     */
    @Log(title = "任务订单记录-任务订单记录所有列表", businessType = BusinessType.SELECT,  isSaveResponseData = false)
    @ApiOperation(value = "任务订单记录所有列表")
    @PostMapping("/getOrderTaskInfoAll")
    public Result<List<GetOrderTaskInfoVO>> getOrderTaskInfoAll(@RequestBody @Valid GetOrderTaskInfoAllDTO dto) {
        GetOrderTaskInfoDO getOrderTaskInfoDO = EntityConversionUtil.copy(dto, GetOrderTaskInfoDO.class);
        List<OrderTaskInfo> orderTaskInfoList = orderTaskInfoService.getALL(getOrderTaskInfoDO);
        List<GetOrderTaskInfoVO> getOrderTaskInfoVOList = EntityConversionUtil.copyList(orderTaskInfoList, GetOrderTaskInfoVO.class);
        List<Long> carIdList = getOrderTaskInfoVOList.stream().map(GetOrderTaskInfoVO::getCarId).collect(Collectors.toList());
        //车辆信息
        List<BaseCarInfo> baseCarInfoList = baseCarInfoService.getCarIdList(carIdList);
        getOrderTaskInfoVOList.forEach(item -> {
            BaseCarInfo baseCarInfo = baseCarInfoList.stream().filter(s -> s.getCarId().equals(item.getCarId())).collect(Collectors.toList()).get(0);
            item.setCarNo(baseCarInfo.getCarNo());
            item.setPlateNumber(baseCarInfo.getPlateNumber());
        });
        return Result.suc(getOrderTaskInfoVOList);
    }

}